<!DOCTYPE html>
<html lang="en" class="coming-soon">
<head>
    <meta charset="utf-8">
    <title>注册</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-touch-fullscreen" content="yes">
    <link type="text/css" href="__PUBLIC__/assets/plugins/iCheck/skins/minimal/blue.css" rel="stylesheet">
    <link type="text/css" href="__PUBLIC__/assets/fonts/font-awesome/css/font-awesome.min.css" rel="stylesheet">
    <link type="text/css" href="__PUBLIC__/assets/fonts/themify-icons/themify-icons.css" rel="stylesheet">
    <link type="text/css" href="__PUBLIC__/assets/css/styles.css" rel="stylesheet">
    <style>
        .text-danger {
            margin-top: 5px;
            display: block;
        }
    </style>
</head>
<body class="focused-form">
<div class="container" id="registration-form">
    <a href="/" class="login-logo"><img src="__PUBLIC__/assets/img/logo-big.png"></a>
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h2>注册</h2>
                </div>
                <div class="panel-body">
                    <form class="form-horizontal" method="post" id="register_post">
                        <div class="form-group mb-md">
                            <label for="username" class="col-xs-4 control-label">用户名</label>
                            <div class="col-xs-8">
                                <input type="text" class="form-control" id="username" required>
                                <span class="text-danger" id="username_error"></span>
                            </div>
                        </div>
                        <div class="form-group mb-md">
                            <label for="Email" class="col-xs-4 control-label">邮箱</label>
                            <div class="col-xs-8">
                                <input type="text" class="form-control" id="email" required>
                                <span class="text-danger" id="email_error"></span>
                            </div>
                        </div>
                        <div class="form-group mb-md">
                            <label for="Password" class="col-xs-4 control-label">密码</label>
                            <div class="col-xs-8">
                                <input type="password" class="form-control" id="password" required>
                                <span class="text-danger" id="password_error"></span>
                            </div>
                        </div>
                        <div class="form-group mb-md">
                            <label for="password_c" class="col-xs-4 control-label">确认密码</label>
                            <div class="col-xs-8">
                                <input type="password" class="form-control" id="password_c" required>
                                <span class="text-danger" id="password_c_error"></span>
                            </div>
                        </div>
                        <div class="form-group mb-md">
                            <label for="age" class="col-xs-4 control-label">年龄</label>
                            <div class="col-xs-8">
                                <input type="tel" class="form-control" id="age" required>
                                <span class="text-danger" id="age_error"></span>
                            </div>
                        </div>
                        <div class="form-group mb-md">
                            <label for="avatar" class="col-xs-4 control-label">头像</label>
                            <div class="col-xs-8">
                                <input type="file" class="form-control" id="avatar" required>
                                <span class="text-danger" id="avatar_error"></span>
                            </div>
                        </div>
                        <div class="form-group mb-md">
                            <label class="col-xs-4 control-label">性别</label>
                            <div class="col-xs-8">
                                <label class="control-label">
                                    <input type="radio" name="sex" value="1" checked>男
                                </label>
                                <label class="control-label">
                                    <input type="radio" name="sex" value="0">女
                                </label>
                            </div>
                        </div>

                        <div class="form-group mb-md col-xs-9 pull-right">
                            <button class="btn btn-block btn-success" type="submit">提交</button>
                        </div>
                    </form>
                </div>
                <div class="panel-footer">
                    <div class="clearfix">
                        <a href="/login" class="btn btn-default pull-left">登录</a>
                        <a href="/register" class="btn btn-primary pull-right">注册</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="__PUBLIC__/assets/js/jquery-3.2.1.min.js"></script>
<script src="__PUBLIC__/assets/js/axios.min.js"></script>
<script src="__PUBLIC__/assets/js/bootstrap.min.js"></script>
<script src="__PUBLIC__/assets/plugins/bootbox/bootbox.js"></script>
<script>
    $(document).ready(function () {
        $('#register_post').submit(function (e) {
            e.preventDefault();
            submit();
        })
    });

    function submit() {
        reset();
        var data = getData();
        if (data === false) {
            return 0;
        }
        axios.post('/admin/user/insertUser', data, {headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
            .then(function (res) {
                if (res.data.success === false) {
                    if (res.data.code === 2000) {
                        for (var key in res.data.data) {
                            showError(key, res.data.data[key]);
                        }
                    }
                } else {
                    bootbox.alert('注册成功', function() {
                        window.location.href = '/login';
                    });
                }
            });
    }

    function reset() {
        var div = $('.has-error');
        div.each(function (key, value) {
            var em = $(value);
            em.removeClass('has-error');
            em.children('span').text('');
        });
    }

    function getData() {
        var username = $('#username');
        var password = $('#password');
        var password_c = $('#password_c');
        var email = $('#email');
        var age = $('#age');
        var avatar = $('#avatar');
        var sex = $('input[type="radio"][checked]');
        var error = null;
        if (username.val().length < 4) {
            error = showError('username', '用户名过短，至少需要4个字节');
        }

        if (password.val().length < 6) {
            error = showError('password', '密码过短，至少需要6个字节');
        }

        if (password.val() !== password_c.val()) {
            error = showError('password_c', '密码不一致');
        }
        var _age = parseInt(age.val());

        if (isNaN(_age) || _age <= 0 || _age > 200) {
            error = showError('age', '年龄必须大于0, 小于200');
        }
        if (error !== null) {
            return false;
        }

        var data = new FormData();
        data.append('username', username.val());
        data.append('email', email.val());
        data.append('password', password.val());
        data.append('password_c', password_c.val());
        data.append('age', age.val());
        data.append('avatar', avatar[0].files[0]);
        data.append('sex', sex.val());
        return data;
    }

    function showError($name, $message) {
        var error = $('#' + $name + '_error');
        error.parent('div').addClass('has-error');
        error.text($message);
        return true;
    }
</script>
</body>
</html>